define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'photo/itinerary/index' + location.search,
                    add_url: 'photo/itinerary/add',
                    edit_url: 'photo/itinerary/edit',
                    del_url: 'photo/itinerary/del',
                    multi_url: 'photo/itinerary/multi',
                    import_url: 'photo/itinerary/import',
                    table: 'photo_itinerary',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'route.name', title: __('Route_id')},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'overtime', title: __('Overtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'Trips', title: __('Trips'), operate: 'LIKE'},
                        {field: 'number', title: __('Number')},
                        {field: 'user.nickname', title: __('Account_id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                         {field: 'number_count', title: __('number_count')},
                          {field: 'file_count', title: __('file_count')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                           {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                 {
                                name: 'edit',
                                text: '上传记录',
                                title: '查看上传记录',
                                classname: 'btn btn-xs btn-success  btn-dialog',
                                url: function (row) {
                                    return "photo/itinerary/index1/itinerary_id/"+row.id;
                                }
    
                            },

                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                that.table = table;


                                $(table).data("operate-edit", true);//隐藏编辑
                                $(table).data("operate-del", true);//隐藏删除
                                return Table.api.formatter.operate.call(that, value, row, index)
                            },

                            // formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
          index1: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'photo/itinerary/index1',
                    edit_url: 'farepair/error/report/edit',
                    del_url: 'farepair/error/report/del'
                }
            });

            var contact  = $("#table");
            

            contact.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar',
                queryParams: function (params) {
                    return params
                },
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        // {field: 'id', title: 'ID', operate: false},
                        {field: 'itinerary_id', title: '行程id', visible: false},
                        {field: 'itinerary.name', title: '行程名称'},
                        {field: 'city', title: __('城市'), operate: false, search: false,formatter: function (val,row,index) {
                     
                            return '<div >' + val + '</div>';
                        }},
                         {field: 'city_id', title: __('城市'), visible: true,
                            addclass: 'selectpage',
                            extend: "data-source='photo/city/index' data-field='name' data-primary-key='id'  " 
                        },
                         {field: 'user.nickname', title: __('昵称'), operate: false, search: false,formatter: function (val,row,index) {
                    
                            return '<div >' + val + '</div>';
                        }},
                        {field: 'user_id', title: __('昵称'), visible: true,
                            addclass: 'selectpage',
                            extend: "data-source='user/user/index' data-field='nickname' data-primary-key='id'  " 
                              
                        },
                        {field: 'user.avatar', title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'file.fullurl', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image}
                       

                    ]
                ],
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
            });
            // 初始化表格
            contact.bootstrapTable();
            var submitForm = function (ids, layero) {
                var options = contact.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
             // 获取选中项
             $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(contact);
                var page = contact.bootstrapTable('getData');
                var all = contact.bootstrapTable('getOptions').totalRows;
              
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("photo/itinerary/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    //btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    btn: ["选中项(" + ids.length + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                       
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            Table.api.bindevent(contact);//当内容渲染完成后
        },
        
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
